package ar.com.photo_admin.service.security

import grails.converters.*
import ar.com.photo_admin.domain.User

class FacebookService {
	
	def facebookGraphService
	
	final static def GRAPH_URL = "https://graph.facebook.com/"
	
	def getProfileByAccesToken(String accessToken) {
		if(accessToken) {
			def urlString = GRAPH_URL + "me/?access_token=" + accessToken

			log.debug urlString
			URL url = new URL(urlString)
			JSON.parse(url.getText())
		}
	}
	
	def getProfileByFacebookUidUsingSession() {
		def facebookData = facebookGraphService.facebookData
		
		if (!facebookData)
			throw new NoFacebookSessionException()	
		
		return facebookGraphService.api(facebookData.uid, facebookData)
	}
	
	def isUserRegistered(String facebookUid) {
		return User.findByFacebookUid(facebookUid) != null
	}
	
	def getProfilePhotoSrc(facebookUserId) {
		facebookGraphService.getProfilePhotoSrc(facebookUserId)
	}
}
